

package com.hertz.iris.authenticator;

import javax.inject.Inject;

import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.github.kevinsawicki.wishlist.Toaster;
import com.hertz.iris.R;
import com.hertz.iris.util.Constants;
import com.hertz.iris.util.Ln;
import com.hertz.iris.util.Strings;
import com.hertz.iris.view.IndexActivity;

/**
 * Bridge class that obtains a API key for the currently configured account
 */
public class OpenIdTokenProvider {

    @Inject AccountManager accountManager;

    /**
     * This call blocks, so shouldn't be called on the UI thread
     *
     * @return API key to be used for authorization with a {@link com.hertz.IrisService.core.BootstrapService} instance
     */
    public String getOpenIdToken(Activity activity) {
    	SharedPreferences sharedPreferences = activity.getSharedPreferences(Constants.MyPreference.PREFERENCE_NAME, Context.MODE_PRIVATE);
    	String authKey = sharedPreferences.getString(Constants.MyPreference.OPEN_ID_TOKEN, "");
    	if(Strings.isBlank(authKey) && !(activity instanceof IndexActivity)){
    		gotoIndexActivity(activity);
    		return "";
    	}else
    		return authKey;
    }
    
    public String getOpenIdToken(Context context){
    	SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.MyPreference.PREFERENCE_NAME, Context.MODE_PRIVATE);
    	String authKey = sharedPreferences.getString(Constants.MyPreference.OPEN_ID_TOKEN, "");
    	return authKey;
    }
    
    public void setOpenIdToken(String openIdToken,String ssoUserName,String ssoSite,Activity activity){
    	Ln.d("setOpenIdToken", "openIdToken=%s,ssoUserName=%s,ssoSite=%s",openIdToken,ssoUserName,ssoSite);
    	SharedPreferences sp = activity.getSharedPreferences(Constants.MyPreference.PREFERENCE_NAME, Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = sp.edit();
    	editor.putString(Constants.MyPreference.OPEN_ID_USERNAME, ssoUserName);
    	editor.putString(Constants.MyPreference.OPEN_ID_SITE, ssoSite);
    	editor.putString(Constants.MyPreference.OPEN_ID_TOKEN, openIdToken);
    	boolean result = editor.commit();
    	if(!result){
    		Toaster.showLong(activity, Constants.ToastMessage.EDITOR_PREFERENCE_AUTHKEY_ERROR);
    	}
    }
    
    public void clearOpenIdToken(Context context){
    	SharedPreferences sp = context.getSharedPreferences(Constants.MyPreference.PREFERENCE_NAME, Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = sp.edit();
    	editor.putString(Constants.MyPreference.OPEN_ID_USERNAME, "");
    	editor.putString(Constants.MyPreference.OPEN_ID_SITE, "");
    	editor.putString(Constants.MyPreference.OPEN_ID_TOKEN, "");
    	boolean result = editor.commit();
    }
    
    public void clearOpenIdTokenAndGotoIndex(Activity activity){
    	Toaster.showLong(activity, R.string.error_http_403);
    	setOpenIdToken("", "", "", activity);
    	gotoIndexActivity(activity);
    }
    
    private void gotoIndexActivity(Activity activity){
    	Intent indexIntent = new Intent(activity,IndexActivity.class);
    	indexIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    	indexIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		activity.startActivity(indexIntent);
		activity.finish();
    }
    
}
